<!DOCTYPE html>
<html lang="en">
<head>
    <title>{{ doc.name if doc.name else "Sales Order" }} - {{ doc.customer_name if doc.customer_name else "Customer" }}</title>
    
    <meta name="description" content="Sales Order for {{ doc.customer_name if doc.customer_name else 'Customer' }}">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta property="og:description" content="Sales Order for {{ doc.customer_name if doc.customer_name else 'Customer' }}">
    <meta property="og:title" content="{{ doc.name if doc.name else 'Sales Order' }}">
    <meta name="twitter:description" content="Sales Order for {{ doc.customer_name if doc.customer_name else 'Customer' }}">
    <meta name="twitter:title" content="{{ doc.name if doc.name else 'Sales Order' }}">
    
    <style>
        a.cta_button{-moz-box-sizing:content-box !important;-webkit-box-sizing:content-box !important;box-sizing:content-box !important;vertical-align:middle}
        .hs-breadcrumb-menu{list-style-type:none;margin:0px 0px 0px 0px;padding:0px 0px 0px 0px}
        .hs-breadcrumb-menu-item{float:left;padding:10px 0px 10px 10px}
        .hs-breadcrumb-menu-divider:before{content:'›';padding-left:10px}
        .hs-featured-image-link{border:0}
        .hs-featured-image{float:right;margin:0 0 20px 20px;max-width:50%}
        @media (max-width: 568px){.hs-featured-image{float:none;margin:0;width:100%;max-width:100%}}
        .hs-screen-reader-text{clip:rect(1px, 1px, 1px, 1px);height:1px;overflow:hidden;position:absolute !important;width:1px}
    </style>

    
    <style>
        .hs-quotes {
            font-family: 'Montserrat', Helvetica, Arial, sans-serif;
            color: #33475b;
        }

        .hs-quotes h1,
        .hs-quotes h2,
        .hs-quotes h3,
        .hs-quotes h4,
        .hs-quotes h5,
        .hs-quotes h6 {
            font-family: 'Montserrat', Helvetica, Arial, sans-serif;
            color: #33475b;
        }

        .hs-quotes--modern .comments-text_wrapper > div {
            border-color: #33475b;
        }

        .hs-quotes--modern .discount-notice {
            color: #33475b;
        }

        .hs-quotes--original .terms_wrapper > div::before {
            background-color: rgba(105, 168, 79, 1.0);
        }

        .hs-quotes--bold {
            font-family: 'Montserrat', Helvetica, Arial, sans-serif;
        }

        .hs-quotes--minimal th,
        .hs-quotes--bold .checkout,
        .hs-quotes--bold .hero__background,
        .hs-quotes--basic .line-items__table th,
        .hs-quotes--basic .totals__row.title,
        .hs-quotes--basic .line-items__total-name,
        .hs-quotes--basic .scheduled-payments__table th {
            background-color: rgba(245, 245, 245, 1.0);
        }

        .hs-quotes--minimal table,
        .hs-quotes--minimal td,
        .hs-quotes--minimal th,
        .hs-quotes .comments,
        .hs-quotes .terms,
        .hs-quotes .annotation {
            border-color: rgba(245, 245, 245, 1.0);
        }

        .hs-quotes .signature__field:before {
            border-color: #33475b;
        }

        .hs-quotes button {
            background: rgba(55, 118, 29, 1.0);
            color: rgba(255, 255, 255, 1.0);
            font-family: 'Montserrat', Helvetica, Arial, sans-serif;
        }

        .hs-quotes #esignature__loading-spinner,
        .hs-quotes .esignature__loading-spinner {
            border-top-color: rgba(105, 168, 79, 1.0) !important;
        }

        .hs-quotes #payment__loading-spinner {
            border-top-color: rgba(55, 118, 29, 1.0) !important;
        }

        .hs-quotes .payment__button--icon {
            fill: rgba(255, 255, 255, 1.0);
        }

        .hs-quotes .banner {
            background-color: rgba(55, 118, 29, 1.0);
            color: white;
            padding: 40px 0;
            margin-bottom: 30px;
        }

        .hs-quotes .banner h1,
        .hs-quotes .banner h2,
        .hs-quotes .banner h3,
        .hs-quotes .banner h4,
        .hs-quotes .banner h5,
        .hs-quotes .banner h6 {
            color: white;
        }

        .banner__content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
        }

        .banner__title {
            font-size: 32px;
            font-weight: 700;
            margin: 0 0 20px 0;
            text-align: center;
            letter-spacing: 1px;
        }

        .banner__subtitle {
            font-size: 18px;
            text-align: center;
            margin: 0 0 30px 0;
            font-weight: 500;
        }

        .banner__info {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            max-width: 800px;
            margin: 0 auto;
        }

        .banner__info-left {
            flex: 1;
        }

        .banner__info-right {
            flex: 1;
            text-align: right;
        }

        .address-section {
            width: 100%;
        }

        .address-type {
            font-size: 14px;
            font-weight: 600;
            margin-bottom: 10px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            opacity: 0.9;
        }

        .customer-info {
            margin-bottom: 20px;
        }

        .customer-name {
            font-size: 18px;
            font-weight: 700;
            margin-bottom: 8px;
        }

        .customer-address {
            font-size: 14px;
            margin-bottom: 4px;
            line-height: 1.4;
        }

        .customer-email {
            font-size: 14px;
            margin-top: 8px;
            font-weight: 500;
        }

        .order-details {
            text-align: right;
        }

        .order-ref {
            font-size: 14px;
            margin-bottom: 4px;
            font-weight: 500;
        }

        .order-date {
            font-size: 14px;
        }

        /* 自定义状态样式 */
        .status-custom {
            padding: 4px 8px;
            border-radius: 4px;
            font-weight: 600;
            font-size: 12px;
            display: inline-block;
        }

        .status-to-manufacturing {
            background-color: #6f42c1;
            color: white;
        }

        .status-to-shipment {
            background-color: #20c997;
            color: white;
        }

        .status-to-deliver {
            background-color: #17a2b8;
            color: white;
        }

        /* 自定义字段样式 */
        .custom-fields-section {
            margin: 20px 0;
            padding: 20px;
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
        }

        .custom-fields-title {
            font-weight: 600;
            font-size: 16px;
            margin-bottom: 15px;
            color: #33475b;
        }

        .custom-fields-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 15px;
        }

        .custom-field {
            display: flex;
            flex-direction: column;
        }

        .custom-field-label {
            font-weight: 600;
            font-size: 12px;
            margin-bottom: 5px;
            color: #33475b;
        }

        .custom-field-value {
            font-size: 12px;
            color: #666;
            padding: 8px;
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .file-attachment {
            color: #007bff;
            text-decoration: underline;
            cursor: pointer;
        }

        .file-attachment:hover {
            color: #0056b3;
        }

        /* 打印样式 */
        @media print {
            .no-print {
                display: none !important;
            }
            
            .custom-fields-section {
                page-break-inside: avoid;
            }
        }
    </style>

    
    <style>
        .line-items__table #line-items__table-body {
            border-top: 2px solid rgba(105, 168, 79, 1.0);
            border-bottom: 2px solid rgba(105, 168, 79, 1.0);
        }
        
        .hs-quotes .esign { display: none; }
        .hs-quotes .payment-schedule { display: none; }
        .hs-quotes .payment { display: none; }

        /* 自定义状态样式 */
        .status-custom {
            padding: 4px 8px;
            border-radius: 4px;
            font-weight: 600;
            font-size: 12px;
            display: inline-block;
        }

        .status-to-manufacturing {
            background-color: #6f42c1;
            color: white;
        }

        .status-to-shipment {
            background-color: #20c997;
            color: white;
        }

        .status-to-deliver {
            background-color: #17a2b8;
            color: white;
        }

        /* 自定义字段样式 */
        .custom-fields-section {
            margin: 20px 0;
            padding: 20px;
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
        }

        .custom-fields-title {
            font-weight: 600;
            font-size: 16px;
            margin-bottom: 15px;
            color: #33475b;
        }

        .custom-fields-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 15px;
        }

        .custom-field {
            display: flex;
            flex-direction: column;
        }

        .custom-field-label {
            font-weight: 600;
            font-size: 12px;
            margin-bottom: 5px;
            color: #33475b;
        }

        .custom-field-value {
            font-size: 12px;
            color: #666;
            padding: 8px;
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .file-attachment {
            color: #007bff;
            text-decoration: underline;
            cursor: pointer;
        }

        .file-attachment:hover {
            color: #0056b3;
        }

        /* 主要内容区域 */
        .quote__content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
        }

        .quote__header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 8px;
        }

        .quote__header-left {
            flex: 1;
        }

        .quote__header-right {
            flex: 1;
            margin-left: 30px;
        }

        .quote__title {
            font-size: 24px;
            font-weight: 700;
            margin: 0 0 15px 0;
            color: #33475b;
        }

        .quote__meta {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
        }

        .quote__meta-item {
            font-size: 14px;
            color: #666;
        }

        .quote__company-info h3 {
            font-size: 18px;
            font-weight: 600;
            margin: 0 0 15px 0;
            color: #33475b;
        }

        .quote__company-details div {
            font-size: 14px;
            color: #666;
            margin-bottom: 5px;
        }

        /* 地址信息 */
        .quote__addresses {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
            margin-bottom: 30px;
        }

        .quote__address {
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid rgba(105, 168, 79, 1.0);
        }

        .quote__address h3 {
            font-size: 16px;
            font-weight: 600;
            margin: 0 0 15px 0;
            color: #33475b;
        }

        .address-content {
            font-size: 14px;
            color: #666;
            line-height: 1.6;
        }

        /* 项目表格 */
        .line-items {
            margin-bottom: 30px;
        }

        .line-items__title {
            font-size: 20px;
            font-weight: 600;
            margin: 0 0 20px 0;
            color: #33475b;
        }

        .line-items__table {
            overflow-x: auto;
        }

        .line-items__table table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .line-items__table th {
            background-color: rgba(105, 168, 79, 1.0);
            color: white;
            font-weight: 600;
            font-size: 12px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            padding: 15px 10px;
            text-align: left;
        }

        .line-items__table td {
            padding: 15px 10px;
            border-bottom: 1px solid #eee;
            font-size: 14px;
            color: #333;
        }

        .line-items__table tr:hover {
            background-color: #f8f9fa;
        }

        .text-right {
            text-align: right;
        }

        .text-center {
            text-align: center;
        }

        .item-name {
            font-weight: 600;
            color: #33475b;
            margin-bottom: 5px;
        }

        .item-description {
            font-size: 12px;
            color: #666;
        }

        .item-code {
            font-size: 11px;
            color: #888;
            font-weight: 500;
            margin-top: 2px;
        }

        .quantity-total {
            display: flex;
            flex-direction: column;
            align-items: flex-end;
        }

        .quantity {
            font-size: 12px;
            color: #666;
            margin-bottom: 2px;
        }

        .total-amount {
            font-weight: 600;
            color: #33475b;
            font-size: 14px;
        }

        /* 评论区域样式 */
        .comments-section {
            margin: 30px 0;
        }

        .comments-box {
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 20px;
            background-color: #f9f9f9;
        }

        .comments-title {
            font-size: 16px;
            font-weight: 600;
            color: #33475b;
            margin: 0 0 15px 0;
        }

        .comments-content {
            font-size: 14px;
            color: #555;
            line-height: 1.5;
            margin-bottom: 15px;
        }

        .terms-content {
            margin-top: 15px;
            padding-top: 15px;
            border-top: 1px solid #ddd;
        }

        .terms-title {
            font-size: 14px;
            font-weight: 600;
            color: #33475b;
            margin-bottom: 8px;
        }

        .terms-text {
            font-size: 13px;
            color: #666;
            line-height: 1.4;
        }

        /* Terms 详细内容展示 */
        .terms-details-section {
            margin: 30px 0;
        }

        .terms-details-box {
            border: 2px solid #000000;
            border-radius: 8px;
            padding: 20px;
            background-color: white;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .terms-details-title {
            font-size: 16px;
            font-weight: 600;
            margin: 0 0 15px 0;
            color: #33475b;
            border-bottom: 1px solid #dee2e6;
            padding-bottom: 10px;
        }

        .terms-details-content {
            font-size: 14px;
            color: #333;
            line-height: 1.6;
            white-space: pre-line;
        }

        .terms-details-content p {
            margin: 0 0 10px 0;
        }

        .terms-details-content p:last-child {
            margin-bottom: 0;
        }

        /* 特殊格式的条款内容 */
        .terms-details-content strong {
            color: #000000;
            font-weight: 600;
        }

        .terms-details-content ul {
            margin: 10px 0;
            padding-left: 20px;
        }

        .terms-details-content li {
            margin: 5px 0;
            color: #555;
        }

        /* 商品图片样式 */
        .item-image-cell {
            text-align: center;
            vertical-align: middle;
            padding: 8px;
        }

        .item-image {
            width: 60px;
            height: 60px;
            object-fit: cover;
            border-radius: 8px;
            border: 2px solid #e9ecef;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .item-image:hover {
            transform: scale(1.1);
            border-color: rgba(105, 168, 79, 1.0);
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
        }

        .no-image-placeholder {
            width: 60px;
            height: 60px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            background-color: #f8f9fa;
            border: 2px dashed #dee2e6;
            border-radius: 8px;
            color: #6c757d;
            font-size: 10px;
            margin: 0 auto;
        }

        .no-image-icon {
            font-size: 20px;
            margin-bottom: 2px;
        }

        .no-image-text {
            font-size: 8px;
            text-align: center;
        }

        /* 图片模态框样式 */
        .image-modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.9);
            z-index: 10000;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
        }

        .image-modal-content {
            max-width: 90%;
            max-height: 90%;
            position: relative;
        }

        .image-modal img {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain;
            border-radius: 8px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.5);
        }

        .image-modal-close {
            position: absolute;
            top: -40px;
            right: 0;
            background: rgba(255,255,255,0.9);
            border: none;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            font-size: 18px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #333;
        }

        .image-modal-close:hover {
            background: white;
            color: #dc3545;
        }

        .image-modal-title {
            position: absolute;
            bottom: -40px;
            left: 0;
            right: 0;
            text-align: center;
            color: white;
            font-size: 16px;
            font-weight: 600;
            background: rgba(0,0,0,0.7);
            padding: 10px;
            border-radius: 4px;
        }

        /* 总计信息 */
        .totals {
            margin-bottom: 30px;
        }

        .totals__table {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .totals__row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 0;
            border-bottom: 1px solid #eee;
        }

        .totals__row:last-child {
            border-bottom: none;
        }

        .totals__row--total {
            font-weight: 700;
            font-size: 16px;
            color: #33475b;
            border-top: 2px solid rgba(105, 168, 79, 1.0);
            margin-top: 10px;
            padding-top: 15px;
        }

        .totals__label {
            font-size: 14px;
            color: #666;
        }

        .totals__amount {
            font-size: 14px;
            color: #333;
            font-weight: 600;
        }

        /* 支付信息 */
        .payment-info {
            margin-bottom: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid rgba(55, 118, 29, 1.0);
        }

        .payment-info h3 {
            font-size: 18px;
            font-weight: 600;
            margin: 0 0 15px 0;
            color: #33475b;
        }

        .payment-info p {
            font-size: 14px;
            color: #666;
            margin: 0 0 10px 0;
        }

        /* 银行信息 */
        .bank-details {
            margin-bottom: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid #007bff;
        }

        /* 付款链接 */
        .payment-link-section {
            margin-bottom: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid #28a745;
        }

        .payment-link-section h3 {
            font-size: 18px;
            font-weight: 600;
            margin: 0 0 15px 0;
            color: #33475b;
        }

        .payment-link-content p {
            font-size: 14px;
            color: #666;
            margin: 0 0 15px 0;
        }

        .payment-link-container {
            text-align: center;
            margin: 20px 0;
        }

        .payment-link-button {
            display: inline-block;
            background: linear-gradient(135deg, #28a745, #20c997);
            color: white;
            text-decoration: none;
            padding: 15px 30px;
            border-radius: 8px;
            font-weight: 600;
            font-size: 16px;
            box-shadow: 0 4px 8px rgba(40, 167, 69, 0.3);
            transition: all 0.3s ease;
            border: none;
            cursor: pointer;
        }

        .payment-link-button:hover {
            background: linear-gradient(135deg, #218838, #1ea085);
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(40, 167, 69, 0.4);
            color: white;
            text-decoration: none;
        }

        .payment-link-button:active {
            transform: translateY(0);
            box-shadow: 0 2px 4px rgba(40, 167, 69, 0.3);
        }

        .payment-note {
            margin-top: 15px;
            padding: 15px;
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            border-radius: 6px;
        }

        .payment-note p {
            margin: 0;
            font-size: 13px;
            color: #856404;
            line-height: 1.4;
        }

        .bank-details h3 {
            font-size: 18px;
            font-weight: 600;
            margin: 0 0 15px 0;
            color: #33475b;
        }

        .bank-info__row {
            display: flex;
            margin-bottom: 10px;
        }

        .bank-info__label {
            font-weight: 600;
            width: 150px;
            font-size: 14px;
            color: #33475b;
        }

        .bank-info__value {
            font-size: 14px;
            color: #666;
            flex: 1;
        }

        /* 条款和条件 */
        .terms {
            margin-bottom: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid #ffc107;
        }

        .terms h3 {
            font-size: 18px;
            font-weight: 600;
            margin: 0 0 15px 0;
            color: #33475b;
        }

        .terms__content {
            font-size: 14px;
            color: #666;
            line-height: 1.6;
        }

        /* 公司信息 */
        .company-info {
            margin-bottom: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid #6c757d;
        }

        .company-info h3 {
            font-size: 18px;
            font-weight: 600;
            margin: 0 0 15px 0;
            color: #33475b;
        }

        .company-details {
            font-size: 14px;
            color: #666;
            line-height: 1.6;
        }

        /* 操作按钮 */
        .quote__actions {
            text-align: center;
            margin: 30px 0;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 8px;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .banner__info {
                flex-direction: column;
                gap: 20px;
            }
            
            .banner__info-right {
                text-align: left;
            }
            
            .banner__title {
                font-size: 28px;
            }
            
            .banner__subtitle {
                font-size: 16px;
            }
            
            .quote__header {
                flex-direction: column;
            }
            
            .quote__header-right {
                margin-left: 0;
                margin-top: 20px;
            }
            
            .quote__addresses {
                grid-template-columns: 1fr;
            }
            
            .custom-fields-grid {
                grid-template-columns: 1fr;
            }
            
            .quote__meta {
                grid-template-columns: 1fr;
            }
            
            .line-items__table {
                font-size: 12px;
            }
            
            .line-items__table th,
            .line-items__table td {
                padding: 10px 5px;
            }
        }

        /* 打印样式 */
        @media print {
            .no-print {
                display: none !important;
            }
            
            .quote__actions {
                display: none !important;
            }
            
            .custom-fields-section {
                page-break-inside: avoid;
            }
            
            .line-items__table {
                page-break-inside: avoid;
            }
            
            body {
                font-size: 12px;
            }
            
            .banner {
                background-color: #f8f9fa !important;
                color: #33475b !important;
            }
            
            .banner h1,
            .banner h2,
            .banner h3,
            .banner h4,
            .banner h5,
            .banner h6 {
                color: #33475b !important;
            }
        }
    </style>

    <style>
        .line-items__table tr.line-items__subheading td {
            font-weight: bold;
        }
    </style>
    
    <meta name="robots" content="noindex, nofollow, noarchive, nosnippet">
</head>

<body>
    <div class="hs-quotes hs-quotes--modern">
        <!-- 获取货币信息 -->
        {% set currency_info = frappe.db.get_value("Currency", doc.currency if doc.currency else 'USD', ["symbol", "symbol_on_right"], as_dict=True) if doc.currency else {"symbol": "$", "symbol_on_right": 0} %}
        {% if currency_info %}
            {% set symbol_on_right = currency_info.symbol_on_right == 1 or currency_info.symbol_on_right == True or currency_info.symbol_on_right == '1' %}
        {% else %}
            {% set symbol_on_right = False %}
        {% endif %}
        
        
        <!-- 头部横幅 -->
        <div class="banner">
            <div class="banner__content">
                <h1 class="banner__title">Order Confirmation</h1>
                <div class="banner__subtitle">
                    {{ doc.name if doc.name else "N/A" }}
                    {% set display_customer_name = doc.customer_name %}
                    {% if not display_customer_name and doc.customer %}
                        {% set customer_doc = frappe.get_doc("Customer", doc.customer) %}
                        {% set display_customer_name = customer_doc.customer_name %}
                    {% endif %}
                    {% if doc.title and doc.title != "{customer_name}" %}
                        - {{ doc.title }}
                    {% endif %}
                </div>
                <div class="banner__info">
                    <div class="banner__info-left">
                        <div class="address-section">
                            <div class="address-type">Billing Address</div>
                            <div class="customer-info">
                                <div class="customer-name">
                                    {% if doc.customer_name %}
                                        {{ doc.customer_name }}
                                    {% elif doc.customer %}
                                        {% set customer_doc = frappe.get_doc("Customer", doc.customer) %}
                                        {{ customer_doc.customer_name if customer_doc.customer_name else doc.customer }}
                                    {% else %}
                                        N/A
                                    {% endif %}
                                </div>
                                {% if doc.customer %}
                                    {% set billing_addresses = frappe.get_all("Address", 
                                        filters=[
                                            ["Dynamic Link", "link_doctype", "=", "Customer"],
                                            ["Dynamic Link", "link_name", "=", doc.customer],
                                            ["address_type", "=", "Billing"],
                                            ["disabled", "=", 0]
                                        ],
                                        pluck="name",
                                        limit=1) %}
                                    {% if billing_addresses %}
                                        {% set billing_address_doc = frappe.get_doc("Address", billing_addresses[0]) %}
                                        {% if billing_address_doc.address_line1 %}
                                            <div class="customer-address">{{ billing_address_doc.address_line1 }}</div>
                                        {% endif %}
                                        {% if billing_address_doc.address_line2 %}
                                            <div class="customer-address">{{ billing_address_doc.address_line2 }}</div>
                                        {% endif %}
                                        {% if billing_address_doc.city %}
                                            <div class="customer-address">{{ billing_address_doc.city }}{% if billing_address_doc.state %}, {{ billing_address_doc.state }}{% endif %}{% if billing_address_doc.pincode %} {{ billing_address_doc.pincode }}{% endif %}</div>
                                        {% endif %}
                                        {% if billing_address_doc.country %}
                                            <div class="customer-address">{{ billing_address_doc.country }}</div>
                                        {% endif %}
                                    {% endif %}
                                {% endif %}
                                {% if doc.contact_email %}
                                    <div class="customer-email">{{ doc.contact_email }}</div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    <div class="banner__info-right">
                        <div class="order-details">
                            <div class="order-ref">Reference: {{ doc.name if doc.name else "N/A" }}</div>
                            <div class="order-date">Created: {{ doc.transaction_date if doc.transaction_date else "N/A" }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
            <!-- Terms 详细内容展示 -->
            {% if doc.terms %}
            <div class="terms-details-section">
                <div class="terms-details-box">
                    <h3 class="terms-details-title">Terms and Conditions Details</h3>
                    <div class="terms-details-content">
                        {{ doc.terms | safe }}
                    </div>
                </div>
            </div>
            {% endif %}
        <!-- 主要内容区域 -->
        <div class="quote__content">
            <!-- 订单信息 -->
            <div class="quote__header">
                <div class="quote__header-left">
                    <h2 class="quote__title">Order Details</h2>
                    <div class="quote__meta">
                        <div class="quote__meta-item">
                            <strong>Order Number:</strong> {{ doc.name if doc.name else "N/A" }}
                        </div>
                        <div class="quote__meta-item">
                            <strong>Order Date:</strong> {{ doc.transaction_date if doc.transaction_date else "N/A" }}
                        </div>
                        <div class="quote__meta-item">
                            <strong>Delivery Date:</strong> {{ doc.delivery_date if doc.delivery_date else "N/A" }}
                        </div>
                        {% if doc.po_no %}
                        <div class="quote__meta-item">
                            <strong>Reference:</strong> {{ doc.po_no }}
                        </div>
                        {% endif %}
                        {% if doc.po_date %}
                        <div class="quote__meta-item">
                            <strong>PO Date:</strong> {{ doc.po_date }}
                        </div>
                        {% endif %}
                    </div>
                </div>
                
                <div class="quote__header-right">
                    <div class="quote__company-info">
                        <h3>Company Information</h3>
                        <div class="quote__company-details">
                            <div><strong>Company:</strong> {{ doc.company if doc.company else "N/A" }}</div>
                            <div><strong>Currency:</strong> {{ doc.currency if doc.currency else "N/A" }}</div>
                            {% if doc.order_type %}
                            <div><strong>Order Type:</strong> {{ doc.order_type }}</div>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
            <!-- 项目表格 -->
            <div class="line-items">
                <h3 class="line-items__title">Products & Services</h3>
                <div class="line-items__table">
                    <table>
                        <thead>
                            <tr>
                                <th style="width:15%;">Image</th>
                                <th style="width:35%;">Item & Description</th>
                                <th style="width:15%;" class="text-right">Unit Price</th>
                                <th style="width:15%;" class="text-right">Discount</th>
                                <th style="width:20%;" class="text-right">Quantity & Total</th>
                            </tr>
                        </thead>
                        <tbody id="line-items__table-body">
                            {% if doc.items %}
                                {% for item in doc.items %}
                                <tr>
                                    <td class="item-image-cell">
                                        {% if item.image %}
                                            <img src="{{ item.image }}" alt="{{ item.item_name if item.item_name else 'Item Image' }}" 
                                                 class="item-image" 
                                                 data-image-src="{{ item.image }}"
                                                 data-image-title="{{ item.item_name if item.item_name else 'Item Image' }}">
                                        {% else %}
                                            <div class="no-image-placeholder">
                                                <span class="no-image-icon">📦</span>
                                                <span class="no-image-text">No Image</span>
                                            </div>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <div class="item-name">{{ item.item_name if item.item_name else "N/A" }}</div>
                                        {% if item.item_code %}
                                            <div class="item-code">{{ item.item_code }}</div>
                                        {% endif %}
                                        {% if item.description %}
                                            <div class="item-description">{{ item.description }}</div>
                                        {% endif %}
                                    </td>
                                    <td class="text-right">
                                        {% if symbol_on_right %}
                                            {{ "{:,.2f}".format(item.rate if item.rate else 0) }} {{ currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD') }}
                                        {% else %}
                                            {{ (currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD')) }}{{ "{:,.2f}".format(item.rate if item.rate else 0) }}
                                        {% endif %}
                                    </td>
                                    <td class="text-right">
                                        {% if item.discount_percentage and item.discount_percentage > 0 %}
                                            {{ "{:.1f}".format(item.discount_percentage) }}%
                                        {% elif item.discount_amount and item.discount_amount > 0 %}
                                            {% if symbol_on_right %}
                                                {{ "{:,.2f}".format(item.discount_amount) }} {{ currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD') }}
                                            {% else %}
                                                {{ (currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD')) }}{{ "{:,.2f}".format(item.discount_amount) }}
                                            {% endif %}
                                        {% else %}
                                            -
                                        {% endif %}
                                    </td>
                                    <td class="text-right">
                                        <div class="quantity-total">
                                            <div class="quantity">
                                                {{ item.qty if item.qty else 0 }} x 
                                                {% if symbol_on_right %}
                                                    {{ "{:,.2f}".format(item.rate if item.rate else 0) }} {{ currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD') }}
                                                {% else %}
                                                    {{ (currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD')) }}{{ "{:,.2f}".format(item.rate if item.rate else 0) }}
                                                {% endif %}
                                            </div>
                                            <div class="total-amount">
                                                {% if symbol_on_right %}
                                                    {{ "{:,.2f}".format(item.amount if item.amount else 0) }} {{ currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD') }}
                                                {% else %}
                                                    {{ (currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD')) }}{{ "{:,.2f}".format(item.amount if item.amount else 0) }}
                                                {% endif %}
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            {% else %}
                                <tr>
                                    <td colspan="5" class="text-center">No items found</td>
                                </tr>
                            {% endif %}
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- 总计信息 -->
            <div class="totals">
                <div class="totals__table">
                    <div class="totals__row">
                        <div class="totals__label">Total Discount</div>
                        <div class="totals__amount">
                            {% if symbol_on_right %}
                                {{ "{:,.2f}".format(doc.discount_amount if doc.discount_amount else 0) }} {{ currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD') }}
                            {% else %}
                                {{ (currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD')) }}{{ "{:,.2f}".format(doc.discount_amount if doc.discount_amount else 0) }}
                            {% endif %}
                        </div>
                    </div>
                    <div class="totals__row">
                        <div class="totals__label">Subtotal</div>
                        <div class="totals__amount">
                            {% if symbol_on_right %}
                                {{ "{:,.2f}".format(doc.net_total if doc.net_total else 0) }} {{ currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD') }}
                            {% else %}
                                {{ (currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD')) }}{{ "{:,.2f}".format(doc.net_total if doc.net_total else 0) }}
                            {% endif %}
                        </div>
                    </div>
                    {% if doc.taxes %}
                        {% for tax in doc.taxes %}
                            <div class="totals__row">
                                <div class="totals__label">Total {{ tax.description if tax.description else (tax.account_head if tax.account_head else "Tax") }}</div>
                                <div class="totals__amount">
                                    {% if symbol_on_right %}
                                        {{ "{:,.2f}".format(tax.tax_amount if tax.tax_amount else 0) }} {{ currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD') }}
                                    {% else %}
                                        {{ (currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD')) }}{{ "{:,.2f}".format(tax.tax_amount if tax.tax_amount else 0) }}
                                    {% endif %}
                                </div>
                            </div>
                        {% endfor %}
                    {% else %}
                        <div class="totals__row">
                            <div class="totals__label">Total Tax</div>
                            <div class="totals__amount">
                                {% if symbol_on_right %}
                                    {{ "{:,.2f}".format(0) }} {{ currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD') }}
                                {% else %}
                                    {{ (currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD')) }}{{ "{:,.2f}".format(0) }}
                                {% endif %}
                            </div>
                        </div>
                    {% endif %}
                    <div class="totals__row totals__row--total">
                        <div class="totals__label">Order Total ({{ doc.currency if doc.currency else "USD" }})</div>
                        <div class="totals__amount">
                            {% if symbol_on_right %}
                                {{ "{:,.2f}".format(doc.grand_total if doc.grand_total else 0) }} {{ currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD') }}
                            {% else %}
                                {{ (currency_info.symbol if currency_info.symbol else (doc.currency if doc.currency else 'USD')) }}{{ "{:,.2f}".format(doc.grand_total if doc.grand_total else 0) }}
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>

            <!-- 支付信息 -->
            <div class="payment-info">
                <h3>Payment Information</h3>
                <p>Please contact us for payment details and terms.</p>
                {% if doc.payment_terms_template %}
                    <p><strong>Payment Terms:</strong> {{ doc.payment_terms_template }}</p>
                {% endif %}
            </div>

            <!-- 银行信息 -->
            {% set company_default_account = frappe.db.get_value("Company", doc.company, "default_bank_account") if doc.company else None %}
            {% if company_default_account %}
                {% set bank_account_name = frappe.db.get_value("Bank Account", {"account": company_default_account, "company": doc.company, "is_company_account": 1}, "name") %}
                {% if bank_account_name and frappe.db.exists("Bank Account", bank_account_name) %}
                    {% set bank_account_doc = frappe.get_doc("Bank Account", bank_account_name) %}
                    {% set bank_doc = frappe.get_doc("Bank", bank_account_doc.bank) if bank_account_doc.bank and frappe.db.exists("Bank", bank_account_doc.bank) else None %}
                <div class="bank-details">
                    <h3>Pay By EFT - Bank Details</h3>
                    <div class="bank-info">
                        {% if bank_doc and bank_doc.bank_name %}
                        <div class="bank-info__row">
                            <div class="bank-info__label">Bank:</div>
                            <div class="bank-info__value">{{ bank_doc.bank_name }}{% if bank_doc.address_html %}, {{ bank_doc.address_html | striptags }}{% endif %}{% if bank_doc.swift_number %}, BIC/SWIFT: {{ bank_doc.swift_number }}{% endif %}</div>
                        </div>
                        {% endif %}
                        {% if bank_account_doc.account_name %}
                        <div class="bank-info__row">
                            <div class="bank-info__label">Account name:</div>
                            <div class="bank-info__value">{{ bank_account_doc.account_name }}</div>
                        </div>
                        {% endif %}
                        {% if bank_account_doc.iban %}
                        <div class="bank-info__row">
                            <div class="bank-info__label">IBAN:</div>
                            <div class="bank-info__value">{{ bank_account_doc.iban }}</div>
                        </div>
                        {% endif %}
                        {% if bank_account_doc.bank_account_no %}
                        <div class="bank-info__row">
                            <div class="bank-info__label">Account Number:</div>
                            <div class="bank-info__value">{{ bank_account_doc.bank_account_no }}{% if bank_account_doc.account_currency %} ({{ bank_account_doc.account_currency }} Only){% endif %}</div>
                        </div>
                        {% endif %}
                        {% if bank_account_doc.branch_code %}
                        <div class="bank-info__row">
                            <div class="bank-info__label">Branch Code:</div>
                            <div class="bank-info__value">{{ bank_account_doc.branch_code }}</div>
                        </div>
                        {% endif %}
                        {% if bank_account_doc.address_html %}
                        <div class="bank-info__row">
                            <div class="bank-info__label">Bank Address:</div>
                            <div class="bank-info__value">{{ bank_account_doc.address_html | safe }}</div>
                        </div>
                        {% endif %}
                    </div>
                </div>
                {% else %}
                <!-- 银行账户不存在时显示默认信息 -->
                <div class="bank-details">
                    <h3>Pay By EFT - Bank Details</h3>
                    <div class="bank-info">
                        <div class="bank-info__row">
                            <div class="bank-info__label">Note:</div>
                            <div class="bank-info__value">Bank account not found. Please contact us for payment details.</div>
                        </div>
                    </div>
                </div>
                {% endif %}
            {% else %}
            <!-- 默认银行信息（当系统没有配置银行信息时显示） -->
            <div class="bank-details">
                <h3>Pay By EFT - Bank Details</h3>
                <div class="bank-info">
                    <div class="bank-info__row">
                        <div class="bank-info__label">Note:</div>
                        <div class="bank-info__value">Please contact us for bank details and payment information.</div>
                    </div>
                </div>
            </div>
            {% endif %}

            <!-- 付款链接 -->
            <div class="payment-link-section">
                <h3>Online Payment</h3>
                <div class="payment-link-content">
                    <p>For quick and secure payment, please use our online payment system:</p>
                    <div class="payment-link-container">
                        <a href="#" class="payment-link-button">
                            💳 Go to Payment Link
                        </a>
                    </div>
                    <div class="payment-note">
                        <p><strong>Note:</strong> If you would like to pay in USD, CNY, CAD, EUR, GBP, please ask your account manager for the payment link or bank details.</p>
                    </div>
                </div>
            </div>

            <!-- 公司信息 -->
            <div class="company-info">
                <h3>{{ doc.company if doc.company else "Company Name" }}</h3>
                <div class="company-details">
                    {{ doc.company_address_display if doc.company_address_display else "Company Address" }}<br>
                    {% if doc.company_contact_person %}
                        Contact: {{ doc.company_contact_person }}<br>
                    {% endif %}
                    {% if doc.contact_phone %}
                        Phone: {{ doc.contact_phone }}<br>
                    {% endif %}
                    {% if doc.contact_email %}
                        Email: {{ doc.contact_email }}
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- 操作按钮 -->
        <div class="quote__actions no-print">
            <button class="cta_button" onclick="window.print()">🖨️ Print Order</button>
            <button class="cta_button" onclick="exportToPDF()">📄 Export PDF</button>
            <button class="cta_button" onclick="exportToExcel()">📊 Export Excel</button>
        </div>
    </div>

    <script>
        // HubSpot Sales Order Template JavaScript

        document.addEventListener('DOMContentLoaded', function() {
            // 初始化模板功能
            initHubSpotTemplate();
            
            // 处理文件附件点击
            initFileAttachments();
            
            // 处理状态显示
            initStatusDisplay();
            
            // 处理自定义字段显示
            initCustomFields();
            
            // 添加打印和导出功能
            addActionButtons();
        });

        function initHubSpotTemplate() {
            console.log('HubSpot Sales Order Template initialized');
            
            // 添加HubSpot样式类
            document.body.classList.add('hs-quotes', 'hs-quotes--modern');
            
            // 初始化响应式功能
            initResponsiveFeatures();
            
            // 初始化图片点击事件
            initImageClickHandlers();
        }

        function initImageClickHandlers() {
            // 为所有商品图片添加点击事件
            const itemImages = document.querySelectorAll('.item-image');
            
            itemImages.forEach(function(img) {
                img.addEventListener('click', function() {
                    const imageSrc = this.getAttribute('data-image-src');
                    const imageTitle = this.getAttribute('data-image-title');
                    if (imageSrc) {
                        openImageModal(imageSrc, imageTitle);
                    }
                });
            });
        }

        function initFileAttachments() {
            // 为所有文件附件添加点击事件
            const fileAttachments = document.querySelectorAll('.file-attachment');
            
            fileAttachments.forEach(function(attachment) {
                attachment.addEventListener('click', function(e) {
                    e.preventDefault();
                    const fileName = this.textContent;
                    openFile(fileName);
                });
                
                // 添加悬停效果
                attachment.addEventListener('mouseenter', function() {
                    this.style.backgroundColor = '#e9ecef';
                    this.style.borderColor = '#007bff';
                });
                
                attachment.addEventListener('mouseleave', function() {
                    this.style.backgroundColor = 'white';
                    this.style.borderColor = '#ddd';
                });
            });
        }

        function initStatusDisplay() {
            // 为自定义状态添加动画效果
            const statusElements = document.querySelectorAll('.status-custom');
            
            statusElements.forEach(function(status) {
                // 添加淡入动画
                status.style.opacity = '0';
                status.style.transition = 'opacity 0.5s ease-in, transform 0.3s ease-in';
                status.style.transform = 'translateY(-10px)';
                
                setTimeout(function() {
                    status.style.opacity = '1';
                    status.style.transform = 'translateY(0)';
                }, 100);
            });
        }

        function initCustomFields() {
            // 为自定义字段添加交互效果
            const customFields = document.querySelectorAll('.custom-field-value');
            
            customFields.forEach(function(field) {
                // 添加悬停效果
                field.addEventListener('mouseenter', function() {
                    this.style.backgroundColor = '#f8f9fa';
                    this.style.borderColor = '#007bff';
                    this.style.transform = 'translateY(-2px)';
                    this.style.boxShadow = '0 4px 8px rgba(0,0,0,0.1)';
                });
                
                field.addEventListener('mouseleave', function() {
                    this.style.backgroundColor = 'white';
                    this.style.borderColor = '#ddd';
                    this.style.transform = 'translateY(0)';
                    this.style.boxShadow = 'none';
                });
            });
        }

        function initResponsiveFeatures() {
            // 处理表格响应式
            const tables = document.querySelectorAll('.line-items__table table');
            tables.forEach(function(table) {
                if (window.innerWidth < 768) {
                    table.style.fontSize = '12px';
                }
            });
            
            // 监听窗口大小变化
            window.addEventListener('resize', function() {
                const tables = document.querySelectorAll('.line-items__table table');
                tables.forEach(function(table) {
                    if (window.innerWidth < 768) {
                        table.style.fontSize = '12px';
                    } else {
                        table.style.fontSize = '14px';
                    }
                });
            });
        }

        function addActionButtons() {
            // 检查是否已经有操作按钮
            if (document.querySelector('.quote__actions')) {
                return;
            }
            
            // 创建操作按钮容器
            const actionsContainer = document.createElement('div');
            actionsContainer.className = 'quote__actions no-print';
            actionsContainer.innerHTML = `
                <button class="cta_button" onclick="window.print()">
                    🖨️ Print Order
                </button>
                <button class="cta_button" onclick="exportToPDF()">
                    📄 Export PDF
                </button>
                <button class="cta_button" onclick="exportToExcel()">
                    📊 Export Excel
                </button>
                <button class="cta_button" onclick="shareOrder()">
                    📤 Share Order
                </button>
            `;
            
            // 插入到页面底部
            const quoteContent = document.querySelector('.quote__content');
            if (quoteContent) {
                quoteContent.appendChild(actionsContainer);
            } else {
                document.body.appendChild(actionsContainer);
            }
        }

        function openFile(fileName) {
            // 模拟文件打开功能
            console.log('Opening file:', fileName);
            
            // 创建文件预览模态框
            const modal = document.createElement('div');
            modal.style.cssText = `
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background-color: rgba(0,0,0,0.8);
                z-index: 10000;
                display: flex;
                justify-content: center;
                align-items: center;
            `;
            
            modal.innerHTML = `
                <div style="
                    background: white;
                    padding: 30px;
                    border-radius: 8px;
                    max-width: 80%;
                    max-height: 80%;
                    overflow: auto;
                ">
                    <h3>File Preview: ${fileName}</h3>
                    <p>This would show the file content for: <strong>${fileName}</strong></p>
                    <p>In a real implementation, this would load the actual file content.</p>
                    <button onclick="this.parentElement.parentElement.remove()" 
                            style="
                                background: #dc3545;
                                color: white;
                                border: none;
                                padding: 10px 20px;
                                border-radius: 4px;
                                cursor: pointer;
                                margin-top: 20px;
                            ">
                        Close
                    </button>
                </div>
            `;
            
            document.body.appendChild(modal);
            
            // 点击背景关闭模态框
            modal.addEventListener('click', function(e) {
                if (e.target === modal) {
                    modal.remove();
                }
            });
        }

        function openImageModal(imageSrc, imageTitle) {
            // 创建图片预览模态框
            const modal = document.createElement('div');
            modal.className = 'image-modal';
            
            modal.innerHTML = `
                <div class="image-modal-content">
                    <button class="image-modal-close" onclick="this.parentElement.parentElement.remove()">×</button>
                    <img src="${imageSrc}" alt="${imageTitle}" onerror="this.parentElement.parentElement.remove(); showNotification('Failed to load image', 'error');">
                    <div class="image-modal-title">${imageTitle}</div>
                </div>
            `;
            
            document.body.appendChild(modal);
            
            // 点击背景关闭模态框
            modal.addEventListener('click', function(e) {
                if (e.target === modal) {
                    modal.remove();
                }
            });
            
            // ESC键关闭模态框
            const handleKeyPress = function(e) {
                if (e.key === 'Escape') {
                    modal.remove();
                    document.removeEventListener('keydown', handleKeyPress);
                }
            };
            document.addEventListener('keydown', handleKeyPress);
        }

        function exportToPDF() {
            // 模拟PDF导出功能
            console.log('Exporting to PDF...');
            
            // 显示加载状态
            showLoadingState('Exporting to PDF...');
            
            setTimeout(function() {
                hideLoadingState();
                showNotification('PDF export completed!', 'success');
            }, 2000);
        }

        function exportToExcel() {
            // 模拟Excel导出功能
            console.log('Exporting to Excel...');
            
            // 显示加载状态
            showLoadingState('Exporting to Excel...');
            
            setTimeout(function() {
                hideLoadingState();
                showNotification('Excel export completed!', 'success');
            }, 2000);
        }

        function shareOrder() {
            // 模拟分享功能
            console.log('Sharing order...');
            
            if (navigator.share) {
                navigator.share({
                    title: 'Sales Order',
                    text: 'Check out this sales order',
                    url: window.location.href
                });
            } else {
                // 复制链接到剪贴板
                navigator.clipboard.writeText(window.location.href).then(function() {
                    showNotification('Order link copied to clipboard!', 'success');
                });
            }
        }

        function showLoadingState(message) {
            const loadingDiv = document.createElement('div');
            loadingDiv.id = 'loading-state';
            loadingDiv.style.cssText = `
                position: fixed;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                background: rgba(0,0,0,0.8);
                color: white;
                padding: 20px 30px;
                border-radius: 8px;
                z-index: 10001;
                font-size: 16px;
            `;
            loadingDiv.innerHTML = `
                <div style="display: flex; align-items: center; gap: 10px;">
                    <div style="
                        width: 20px;
                        height: 20px;
                        border: 2px solid #fff;
                        border-top: 2px solid transparent;
                        border-radius: 50%;
                        animation: spin 1s linear infinite;
                    "></div>
                    ${message}
                </div>
            `;
            
            // 添加旋转动画
            const style = document.createElement('style');
            style.textContent = `
                @keyframes spin {
                    0% { transform: rotate(0deg); }
                    100% { transform: rotate(360deg); }
                }
            `;
            document.head.appendChild(style);
            
            document.body.appendChild(loadingDiv);
        }

        function hideLoadingState() {
            const loadingDiv = document.getElementById('loading-state');
            if (loadingDiv) {
                loadingDiv.remove();
            }
        }

        function showNotification(message, type = 'info') {
            const notification = document.createElement('div');
            notification.style.cssText = `
                position: fixed;
                top: 20px;
                right: 20px;
                background: ${type === 'success' ? '#28a745' : type === 'error' ? '#dc3545' : '#007bff'};
                color: white;
                padding: 15px 20px;
                border-radius: 4px;
                z-index: 10002;
                font-size: 14px;
                box-shadow: 0 4px 8px rgba(0,0,0,0.2);
                animation: slideIn 0.3s ease-out;
            `;
            notification.textContent = message;
            
            // 添加滑入动画
            const style = document.createElement('style');
            style.textContent = `
                @keyframes slideIn {
                    from { transform: translateX(100%); opacity: 0; }
                    to { transform: translateX(0); opacity: 1; }
                }
            `;
            document.head.appendChild(style);
            
            document.body.appendChild(notification);
            
            // 3秒后自动移除
            setTimeout(function() {
                notification.style.animation = 'slideIn 0.3s ease-out reverse';
                setTimeout(function() {
                    notification.remove();
                }, 300);
            }, 3000);
        }

        // 工具函数
        function formatCurrency(amount, currency = 'USD') {
            return new Intl.NumberFormat('en-US', {
                style: 'currency',
                currency: currency
            }).format(amount);
        }

        // 货币信息缓存
        let currencyCache = {};

        // 获取货币信息（从currency表）
        async function getCurrencyInfo(currencyCode) {
            if (currencyCache[currencyCode]) {
                return currencyCache[currencyCode];
            }
            
            try {
                // 这里应该调用后端API获取货币信息
                // 由于这是前端模板，我们使用默认值
                const defaultCurrencyInfo = {
                    symbol: currencyCode === 'USD' ? '$' : currencyCode,
                    symbol_on_right: false,
                    number_format: '#,###.##'
                };
                
                currencyCache[currencyCode] = defaultCurrencyInfo;
                return defaultCurrencyInfo;
            } catch (error) {
                console.error('Error fetching currency info:', error);
                return {
                    symbol: currencyCode,
                    symbol_on_right: false,
                    number_format: '#,###.##'
                };
            }
        }

        // 格式化货币显示（用于模板）
        async function format_currency(amount, currency = 'USD') {
            if (!amount || isNaN(amount)) return '0.00';
            
            const currencyInfo = await getCurrencyInfo(currency);
            const symbol = currencyInfo.symbol || currency;
            const formattedAmount = parseFloat(amount).toFixed(2);
            
            // 根据symbol_on_right字段决定符号位置
            if (currencyInfo.symbol_on_right) {
                return formattedAmount + ' ' + symbol;
            } else {
                return symbol + formattedAmount;
            }
        }

        function formatDate(dateString) {
            if (!dateString) return 'N/A';
            
            const date = new Date(dateString);
            return date.toLocaleDateString('en-US', {
                year: 'numeric',
                month: 'short',
                day: 'numeric'
            });
        }

        // 打印优化
        window.addEventListener('beforeprint', function() {
            // 打印前隐藏不需要的元素
            const noPrintElements = document.querySelectorAll('.no-print');
            noPrintElements.forEach(function(element) {
                element.style.display = 'none';
            });
        });

        window.addEventListener('afterprint', function() {
            // 打印后恢复元素显示
            const noPrintElements = document.querySelectorAll('.no-print');
            noPrintElements.forEach(function(element) {
                element.style.display = '';
            });
        });

        // 键盘快捷键
        document.addEventListener('keydown', function(e) {
            // Ctrl+P 打印
            if (e.ctrlKey && e.key === 'p') {
                e.preventDefault();
                window.print();
            }
            
            // Ctrl+S 保存/导出
            if (e.ctrlKey && e.key === 's') {
                e.preventDefault();
                exportToPDF();
            }
            
            // Escape 关闭模态框
            if (e.key === 'Escape') {
                const modals = document.querySelectorAll('[style*="position: fixed"]');
                modals.forEach(function(modal) {
                    if (modal.id !== 'loading-state') {
                        modal.remove();
                    }
                });
            }
        });
    </script>
</body>
</html>
